package org.apache.dolphinscheduler.api.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation;
import org.apache.dolphinscheduler.api.aspect.PermissionCheck;
import org.apache.dolphinscheduler.api.exceptions.ApiException;
import org.apache.dolphinscheduler.api.service.CouponbillService;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.constants.Constants;
import org.apache.dolphinscheduler.dao.entity.Bill;
import org.apache.dolphinscheduler.dao.entity.Couponbill;
import org.apache.dolphinscheduler.dao.entity.Coupondetail;
import org.apache.dolphinscheduler.dao.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

import java.math.BigDecimal;
import java.util.Map;

import static org.apache.dolphinscheduler.api.enums.Status.LOGIN_USER_QUERY_BILL_LIST_PAGING_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.LOGIN_USER_QUERY_BILL_TOTAL_ERROR;

/**
 * <p>
 * 收支明细表 前端控制器
 * </p>
 *
 * @author denggh
 * @since 2023-08-14
 */
@Api(tags = "算力券订单明细 API")
@RestController
@RequestMapping("/couponbill")
public class CouponbillController extends BaseController {

    @Autowired
    private CouponbillService CouponbillService;

    /**
     * 获取算力券订单明细列表
     */
    @GetMapping()
    @ResponseStatus(HttpStatus.OK)
    @ApiException(LOGIN_USER_QUERY_BILL_LIST_PAGING_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @ApiOperation("获取算力券订单明细列表")
    @PermissionCheck(permission = "operation:coupon:list")
    public Result list(
                       @Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                       @RequestParam(value = "id", required = false)Integer id,
                       @RequestParam(value = "couponName", required = false) String couponName,
                       @RequestParam(value = "couponState", required = false) String couponState,
                       @RequestParam(value = "billNo", required = false) String billNo,
                       @RequestParam(value = "userAccount", required = false) String userAccount,
                       @RequestParam(value = "issueType", required = false) String issueType,
                       @RequestParam(value = "startDateClaim", required = false) String startDateClaim,
                       @RequestParam(value = "endDateClaim", required = false) String endDateClaim,
                       @RequestParam(value = "startDateUse", required = false) String startDateUse,
                       @RequestParam(value = "endDateUse", required = false) String endDateUse,
                       @RequestParam(value = "startDateEnd", required = false) String startDateEnd,
                       @RequestParam(value = "endDateEnd", required = false) String endDateEnd,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam("pageNo") Integer pageNo) {
        Result result = checkPageParams(pageNo, pageSize);
        if (!result.checkResult()) {
            return result;
        }
        Couponbill couponbill= new Couponbill();
        couponbill.setCoupondetailId(id);
        couponbill.setCouponName(couponName);
        couponbill.setCouponState(couponState);
        couponbill.setBillNo(billNo);
        couponbill.setUseAccount(userAccount);
        return CouponbillService.selectPageCouponbillList(loginUser, couponbill, issueType, startDateClaim, endDateClaim, startDateUse, endDateUse, startDateEnd, endDateEnd, pageSize, pageNo);
    }

    /**
     * 获取算力券信息列表
     */
    @GetMapping("/rule")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(LOGIN_USER_QUERY_BILL_LIST_PAGING_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @ApiOperation("获取算力券订单明细列表")
    @PermissionCheck(permission = "operation:coupon:list")
    public Result list(
            //@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
            @RequestParam(value = "id", required = true) Integer id) {
        return CouponbillService.selectCouponRule(id);
    }

    /**
     * 获取算力券订单明细列表
     */
    @GetMapping("/get/couponbill/table")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(LOGIN_USER_QUERY_BILL_LIST_PAGING_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @ApiOperation("获取算力券明细统计表")
    @PermissionCheck(permission = "operation:coupon:list")
    public Result getCouponbillTable(
            @RequestParam(value = "coupondetailId", required = true) Integer coupondetailId) {
        return CouponbillService.getCouponbillTable(coupondetailId);
    }

    /**
     * 获取用户算力券
     */
    @GetMapping("/get/couponbill/style")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(LOGIN_USER_QUERY_BILL_LIST_PAGING_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @ApiOperation("获取用户算力券")
    @PermissionCheck(permission = "operation:coupon:query")
    public Result getCouponbillStyle(
            @Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
            @RequestParam(value = "searchType", required = true) Integer searchType) {

        return CouponbillService.getUserCouponbills(loginUser, searchType);
    }

    /**
     * 获取用户订单可用算力券
     */
    @GetMapping("/getAvailableCoupon")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(LOGIN_USER_QUERY_BILL_LIST_PAGING_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @ApiOperation("获取用户订单可用算力券")
    @PermissionCheck(permission = "operation:coupon:query-available")
    public Result getAvailableCoupon(
        @Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
        @RequestParam(value = "amount", required = true) Integer amount,
        @RequestParam(value = "billNo", required = true) String billNo,
        @RequestParam(value = "billType", required = true) Integer billType) {

        return CouponbillService.getAvailableCoupon(loginUser, amount, billNo, billType);
    }

    /**
     * 导出算力券订单明细列表
     */
    @ResponseStatus(HttpStatus.OK)
    @GetMapping("/export")
    @ApiException(LOGIN_USER_QUERY_BILL_TOTAL_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @ApiOperation("导出算力券明细列表")
    @PermissionCheck(permission = "operation:coupon:export")
    public void export(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                       @RequestParam(value = "id", required = false)Integer id,
                       @RequestParam(value = "couponState", required = false) String couponState,
                       @RequestParam(value = "billNo", required = false) String billNo,
                       @RequestParam(value = "userAccount", required = false) String userAccount,
                       @RequestParam(value = "startDateClaim", required = false) String startDateClaim,
                       @RequestParam(value = "endDateClaim", required = false) String endDateClaim,
                       @RequestParam(value = "startDateUse", required = false) String startDateUse,
                       @RequestParam(value = "endDateUse", required = false) String endDateUse,
                       @RequestParam(value = "startDateEnd", required = false) String startDateEnd,
                       @RequestParam(value = "endDateEnd", required = false) String endDateEnd,
                       HttpServletResponse response) {
        Couponbill couponbill= new Couponbill();
        couponbill.setCoupondetailId(id);
        couponbill.setCouponState(couponState);
        couponbill.setBillNo(billNo);
        couponbill.setUseAccount(userAccount);
        CouponbillService.exportExcel(loginUser, couponbill, startDateClaim, endDateClaim, startDateUse, endDateUse, startDateEnd, endDateEnd, response);
    }


    /**
     * 获取算力券使用数量统计
     */
    @GetMapping("/getCouponUsedTotal")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(LOGIN_USER_QUERY_BILL_TOTAL_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @ApiOperation("获取算力券使用类型数量")
    @PermissionCheck(permission = "operation:coupon:detail-graph")
    public Result getCouponUsedTotal(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                                     @RequestParam(value = "month", required = true) String month) {
        return CouponbillService.selectCouponUsedTotal(loginUser, month);
    }

}
